/*
GANESHA - Reasoning on critical infrastructures affected by emergency situations
Copyright (c) 2010-2011, Information Eng. Research Unit - Univ. of Alcala
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package es.uah.cc.ie.ganesha.messaging.tso;

import es.uah.cc.ie.ganesha.SituationManager;
import es.uah.cc.ie.ganesha.messaging.AlertMessage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tacticalsituationobject.schemas.tso.x20.TSO20Document;

/**
 *
 * @author ieru
 */
public class TSOSituationManager extends SituationManager {

	/**
	 * Adds a new TSO message by parsing it from a XML file.
	 * @param path XML file path where the message will be obtained from.
	 */
	public void addMessageFromXML(String path)
	{
		try {
			TSO20Document document = TSO20Document.Factory.parse(new File(path));
			TSOMessage message = new TSOMessage(document.getTSO20());
			_messages.put(message.getIdentifier(), message);
		} catch (Exception ex) {
			Logger.getLogger(TSOSituationManager.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	@Override
	public void dumpSituation(DataOutputStream o) {

		for(AlertMessage message : _messages.values())
		{
			TSO20Document document = TSO20Document.Factory.newInstance();

			document.setTSO20(((TSOMessage) message).toXMLObject());
			
			try {
				o.writeUTF(document.toString());
			} catch (IOException ex) {
				Logger.getLogger(TSOSituationManager.class.getName()).log(Level.SEVERE, null, ex);
			}
		}

	}

	/**
	 * Creates a new AlertMessage according to the type of situations managed (so TSOMessage in this case).
	 * @param identifier Identifier of the new message.
	 * @return The new message.
	 */
	@Override
	public AlertMessage newMessage(String identifier)
	{
		return new TSOMessage(identifier);
	}

	/**
	 * Returns the descriptive domain from the non-descriptive domain.
	 * @param domain Non-descriptive domain.
	 * @return Descriptive domain.
	 */
	@Override
	public String getEventTypeFromDomain(String domain) {
		if(domain.equals("ELECTRICIDAD"))//RED ELECTRICA ESPANYOLA"))
		{
			return "/DIS/PWROUT";///ELECTRICIDAD";
		} else if(domain.equals("GAS"))//ENAGAS"))
		{
			return "/DIS";///GAS";
		}

		return "";
	}
}
